var _homeView;

app.Views.HomeView = Backbone.View.extend({

	initialize:function () {

		_homeView = this;

		this.el = $("#wrapper");
		this.template = _.template(tpl.get('home'));
		this.delegateEvents();

		// me
		facebook.me = new facebook.Models.Me();
		facebook.me.on(facebook.Events.COMPLETE,this.onCompleteMe);
		facebook.me.on(facebook.Events.ERROR,this.onErrorMe);

		this.render();

        return this;
    },

    render:function (eventName) {

        $(this.el).html(this.template());

		$("#home").bind("click",this.goToHome);
		$("#upload").bind("click",this.uploadPicture);
		$("#share").bind("click",this.share);
		$("#invite").bind("click",this.invite);
		$("#autoShare").bind("click",this.autoShare);

        return this;
    },

    goToHome:function(){

    	app.routeur.navigate("home", true);
    	return false;

    },

    uploadPicture:function(){

    	console.log("uploadPhoto");

		var attachment = {
			src:"http://3.bp.blogspot.com/-5U8HwkvTgvs/Te33rIA12JI/AAAAAAAAOP4/UB3XuHYXmcM/s1600/Google.jpg",
			description:"Test Description",
			albumId:null,

		};

    facebook.uploadPicture(attachment);
		facebook.on(facebook.Events.UPLOAD_PICTURE_COMPLETE,_homeView.onCompleteUploadPicture);
		facebook.on(facebook.Events.UPLOAD_PICTURE_ERROR,_homeView.onErrorUploadPicture);

    	return false;

    },

     onErrorUploadPicture:function(){

     	console.log("upload picture error");

    },

    onCompleteUploadPicture:function(){

    	console.log("upload picture complete");

    },

    /*createAlbum:function(){

    	facebook.createAlbum("test album name","test album description");
		facebook.on(facebook.Events.ALBUM_COMPLETE,_homeView.onCompleteCreateAlbum);
		facebook.on(facebook.Events.ALBUM_ERROR,_homeView.onErrorCreateAlbum);
    	return false;

    },

    onErrorCreateAlbum:function(){

    	console.log("create album error");

    },

    onCompleteCreateAlbum:function(){

    	console.log("create album success : ID > "+facebook.lastCreateAlbumId);

    },*/

	onErrorMe:function(){

		console.log("error with facebook.me.fetch()");

	},

	onCompleteMe:function(context){

		app.views.profileView = new app.Views.ProfileView({model:facebook.me});
		$("#wrapper").append(app.views.profileView.render().el);

		// permissions
		facebook.permissions = new facebook.Models.Permissions();
		facebook.permissions.on(facebook.Events.COMPLETE,_homeView.onCompletePermissions);
		facebook.permissions.on(facebook.Events.ERROR,_homeView.onErrorPermissions);

	},

	onErrorPermissions:function(){

		console.log("error with facebook.permissions.fetch()");

	},

	onCompletePermissions:function(){

		app.views.permissionsView = new app.Views.PermissionsView({model:facebook.permissions});
		$("#wrapper").append(app.views.permissionsView.render().el);


		var perm = _.pairs(facebook.permissions.attributes);

		_.each(perm, function (permission) {

			  console.log(permission[0]+" -> "+permission[1]);

		},this);

		// friends
		facebook.friends = new facebook.Collections.Friends();
		facebook.friends.on(facebook.Events.COMPLETE,_homeView.onCompleteFriends);
		facebook.friends.on(facebook.Events.ERROR,_homeView.onErrorFriends);

	},

	onErrorFriends:function(){

		console.log("error with facebook.friends.fetch()");

	},

	onCompleteFriends:function(){

		app.views.listFriendsView = new app.Views.ListFriendsView({collection:facebook.friends});
		$("#wrapper").append(app.views.listFriendsView.render().el);

		// albums
		facebook.albums = new facebook.Collections.Albums();
		facebook.albums.on(facebook.Events.COMPLETE,_homeView.onCompleteAlbums);
		facebook.albums.on(facebook.Events.ERROR,_homeView.onErrorAlbums);

	},

	onErrorAlbums:function(){

		console.log("error with facebook.albums.fetch()");

	},

	onCompleteAlbums:function(){

		//console.log(facebook.albums);
		app.views.listAlbumsView = new app.Views.ListAlbumsView({collection:facebook.albums});
		$("#wrapper").append(app.views.listAlbumsView.render().el);

		// feed
		facebook.feed = new facebook.Collections.Feed();
		facebook.feed.on(facebook.Events.COMPLETE,_homeView.onCompleteFeed);
		facebook.feed.on(facebook.Events.ERROR,_homeView.onErrorFeed);


	},

	onErrorFeed:function(){

		console.log("error with facebook.feed.fetch()");

	},

	onCompleteFeed:function(){

		_.each(facebook.feed.models, function (feed) {

			  if (feed.get("story")) console.log(feed.get("story"));

		},this);

	},

	autoShare:function(){

		var attachment = {
			message:"Je viens de m'inscrire blabla...",
			picture:"http://greenlime.fr/images/logoBgWhite.png",
			link:"http://www.google.fr",
			name:"Message automatique",
			caption:"Envoyé par l'application",
			description:"Ceci est une alerte, detendez vous !",
			source:null,
			place:null,
			tags:null
		};

		facebook.setStatutMessage(attachment);
		facebook.on(facebook.Events.SET_STATUT_MESSAGE_COMPLETE, _homeView.setStatutMessageComplete);
		facebook.on(facebook.Events.SET_STATUT_MESSAGE_ERROR, _homeView.setStatutMessageError);

		return false;

	},

	setStatutMessageError:function(response){

		facebook.off(facebook.Events.SET_STATUT_MESSAGE_COMPLETE, _homeView.setStatutMessageComplete);
		facebook.off(facebook.Events.SET_STATUT_MESSAGE_ERROR, _homeView.setStatutMessageError);

		console.log(response);
		console.log("automatic post doesn't send !");

	},

	setStatutMessageComplete:function(response){

		facebook.off(facebook.Events.SET_STATUT_MESSAGE_COMPLETE, _homeView.setStatutMessageComplete);
		facebook.off(facebook.Events.SET_STATUT_MESSAGE_ERROR, _homeView.setStatutMessageError);

		console.log(response);
		console.log("automatic post send !");

	},

	share:function(){

		var attachment = {
			titre:"Facebook Dialogs",
			caption:"Reference Documentation",
			desc:"Dialogs provide a simple, consistent interface for applications to interface with users.",
			link:"https://developers.facebook.com/docs/reference/dialogs/",
			picture:"http://fbrell.com/f8.jpg"
		}
		facebook.share(attachment);
		facebook.on(facebook.Events.SHARE_COMPLETE, _homeView.shareComplete);
		facebook.on(facebook.Events.SHARE_ERROR, _homeView.shareError);
		return false;

	},

	shareComplete:function(response){

		facebook.off(facebook.Events.SHARE_COMPLETE, _homeView.shareComplete);
		facebook.off(facebook.Events.SHARE_ERROR, _homeView.shareError);

		console.log(response);
		console.log("feed post !");

	},

	shareError:function(response){

		facebook.off(facebook.Events.SHARE_COMPLETE, _homeView.shareComplete);
		facebook.off(facebook.Events.SHARE_ERROR, _homeView.shareError);

		console.log(response);
		console.log("feed don't post !");

	},

	invite:function(){

		var attachment = {
			msg:"A message"
		}
		facebook.invite(attachment);
		facebook.on(facebook.Events.INVITE_COMPLETE, _homeView.inviteComplete);
		facebook.on(facebook.Events.INVITE_ERROR, _homeView.inviteError);
		return false;

	},

	inviteComplete:function(response){

		facebook.off(facebook.Events.INVITE_COMPLETE, _homeView.inviteComplete);
		facebook.off(facebook.Events.INVITE_ERROR, _homeView.inviteError);

		console.log(response);
		console.log("invitation(s) send !");

	},

	inviteError:function(response){

		facebook.off(facebook.Events.INVITE_COMPLETE, _homeView.inviteComplete);
		facebook.off(facebook.Events.INVITE_ERROR, _homeView.inviteError);

		console.log(response);
		console.log("invitation(s) don't send !");

	},

	destroy:function(){

		$(this.el).empty();

	}

});